package com.stagewebserver.webserver.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stagewebserver.webserver.mapper.MenusMapper;
import com.stagewebserver.webserver.service.MenusService;
import com.stagecommon.common.pojo.Menus;
import com.stagecommon.common.pojo.RespBean;
import com.stagecommon.common.vo.MenusVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: yxl
 * @since: 2022-01-29
 **/
@Slf4j
@Service
public class MenusServiceImpl extends ServiceImpl<MenusMapper, Menus> implements MenusService {

    @Autowired
    private MenusMapper menusMapper;

    @Override
    public RespBean<List<MenusVo>> getMenusList() {
        return RespBean.operationSuccess("查询成功",200,menusMapper.selectMenusList(0));
    }

    @Override
    public RespBean<IPage<Menus>> getMenus(Integer currentPage, Integer size, Menus menus) {
        Page<Menus> menusPage = new Page<>(currentPage, size);
        return RespBean.operationSuccess("查询成功",200,menusMapper.selectAllMenus(menus.getMenuName(), menusPage));
    }

    @Override
    public RespBean<List<Menus>> getMenusByLevel(Byte menuLevel) {
        return RespBean.operationSuccess("查询成功", 200, menusMapper.selectMenusByLevel(menuLevel));
    }

    @Override
    public RespBean addMenu(Menus menus) {
        menus.setCreatedTime(LocalDateTime.now());
        if (menusMapper.insert(menus) == 1){
            return RespBean.operationSuccess("添加权限成功！", 200, 1);
        }
        return RespBean.operationFail("添加权限失败");
    }

    @Override
    public RespBean<Menus> getMenuById(Integer menuId) {
        return RespBean.operationSuccess("查询成功", 200, menusMapper.selectById(menuId));
    }

    @Override
    public RespBean<Integer> deleteMenuById(Integer menuId) {
        if (menusMapper.deleteById(menuId) == 1){
            return RespBean.operationSuccess("删除权限成功！", 200, 1);
        }
        return RespBean.operationFail("删除权限失败！");
    }

    @Override
    public RespBean<Integer> updateMenuById(Menus menus) {
        if (menusMapper.updateById(menus) == 1){
            return RespBean.operationSuccess("更新权限成功！", 200, 1);
        }
        return RespBean.operationFail("更新权限失败！");
    }
}
